﻿using System;
using System.Collections.Generic;
using System.Text;
using DotRas;
using System.Collections.ObjectModel;

namespace TrafficSplitter
{
    class AdapterFunctionsRAS : AdapterFunctionsInterface
    {
        public RasConnection FindThisActiveConnection(VirtualAdapter adapter)
        {
            ReadOnlyCollection<RasConnection> ret;
            ret = RasConnection.GetActiveConnections();
            foreach (RasConnection entry in ret)
            {
                if (entry.EntryName.Equals(adapter.GetEntryName(), StringComparison.OrdinalIgnoreCase))
                    return entry;
            }

            return null;
        }

        public String GetGatewayIPAddress(VirtualAdapter adapter)
        {
            Routing.FindIndex(adapter);
            Routing.GetAdapterInfo(adapter);
            if (adapter.GetGateway() == null)
            {
                adapter.SetGateway("0.0.0.0");
            }
            return adapter.GetGateway();
        }

        public void EnumerateInterfaces(List<VirtualAdapter> adapters)
        {
            Enumerate(adapters, true);
            Enumerate(adapters, false);
        }

        private void Enumerate(List<VirtualAdapter> adapters, Boolean currentUser)
        {
            RasPhoneBook pb = new RasPhoneBook();
            try
            {
                pb.Open(currentUser);
            }
            catch (Exception e)
            {
                if (currentUser)
                    Constants.AppendToLog(LOG_TYPE.Debug, "Unable to open phone book for current user");
                else
                    Constants.AppendToLog(LOG_TYPE.Debug, "Unable to open phone book for all users");

                Constants.AppendToLog(LOG_TYPE.Debug, e.Message);
            }

            foreach (RasEntry entry in pb.Entries)
            {
                adapters.Add(new VirtualAdapter(entry.Name, 0, TS_ADAPTER_TYPES.RASDevice, currentUser ? 0 : 1));
            }
        }

        public TS_STATES GetInterfaceState(VirtualAdapter adapter)
        {
            RasConnection conn = this.FindThisActiveConnection(adapter);
            if (conn != null)
            {
                RasConnectionStatus status = conn.GetConnectionStatus();
                RasConnectionState state = status.ConnectionState;
                switch (state) {
                    case RasConnectionState.Connected:
                        return TS_STATES.Connected;

                    case RasConnectionState.Disconnected:
                        return TS_STATES.Disconnected;

                    case RasConnectionState.PortOpened:
                    case RasConnectionState.ConnectDevice:
                    case RasConnectionState.DeviceConnected:
                    case RasConnectionState.AllDevicesConnected:
                    case RasConnectionState.Authenticate:
                    case RasConnectionState.AuthNotify:
                    case RasConnectionState.AuthProject:
                    case RasConnectionState.Projected:
                    case RasConnectionState.Authenticated:
                        return TS_STATES.Dialing;

                    default:
                        return TS_STATES.Unknown;
                }

            }

            return TS_STATES.Disconnected;
        }
    }
}
